package com.helen.ui.order;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;

import com.helen.adapter.OrderFragmentAdapter;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.ui.BaseFragment;
import com.helen.utils.MyLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by yang on 2018/4/16.
 * 我的订单
 */

public class OrderActivity extends BaseActivity {
    @BindView(R.id.tl_order)
    TabLayout tlOrder;
    @BindView(R.id.vp_order)
    ViewPager vpOrder;
    private List<BaseFragment> mFragments = new ArrayList<>();
    private String[] stringTabs;
    private int currentId = 0;
    private OrderFragmentAdapter mFragmentAdapter;
    public static String orderAction = "order.action.reload.fragment";

    private BroadcastReceiver reloadReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(orderAction)) {
                currentId = intent.getIntExtra("currentId", 0);
                MyLog.e("yang", "广播currentId====" + currentId);
                vpOrder.setCurrentItem(currentId);
                mFragmentAdapter.reloadFragment(currentId);
            }
        }
    };
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_order;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.my_order);
        currentId = getIntent().getIntExtra("currentId", 0);
        addTabTitle();
        initViewPager();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(orderAction);
        registerReceiver(reloadReceiver, intentFilter);
    }

    @Override
    protected void updateViews() {

    }


    private void addTabTitle() {
        stringTabs = getResources().getStringArray(R.array.work_order_tabs);
        for (int i = 0; i < stringTabs.length; i++) {
            tlOrder.addTab(tlOrder.newTab().setText(stringTabs[i]));
        }
        tlOrder.setTabMode(TabLayout.MODE_SCROLLABLE);

    }

    private void initViewPager() {
        for (int i = 0; i < stringTabs.length; i++) {
            OrderFragment orderBaseFragment = new OrderFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("currentId", i);
            orderBaseFragment.setArguments(bundle);
            mFragments.add(orderBaseFragment);
        }
        mFragmentAdapter = new OrderFragmentAdapter(getSupportFragmentManager(), mFragments, stringTabs);
        vpOrder.setAdapter(mFragmentAdapter);
        vpOrder.setOffscreenPageLimit(stringTabs.length);
        tlOrder.setupWithViewPager(vpOrder);
        vpOrder.setCurrentItem(currentId);
        vpOrder.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentId = position;
                mFragmentAdapter.reloadFragment(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(reloadReceiver);
    }

}
